package com.yc.navy.goods.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.goods.bean.BalanceInfo;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.service.IBalanceInfoService;
import com.yc.navy.goods.vo.ResultVO;

@RestController
@RequestMapping("/balance")
public class BalanceInfoController {
	@Autowired
	private IBalanceInfoService BalanceInfoService;
	
	@RequestMapping("/find")
	public ResultVO find(Integer pid) {
		if(String.valueOf(pid) == null || "".equals(String.valueOf(pid))) {
			return new ResultVO(ResultEnum.NOT_DATA);
		}
		List<BalanceInfo> list = BalanceInfoService.find(pid);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/findList")
	public ResultVO findList() {
		List<BalanceInfo> list = BalanceInfoService.findList();
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.NOT_DATA);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@Transactional
	@RequestMapping("/addNum")
	public ResultVO addNum(BalanceInfo balance) {
		int result = BalanceInfoService.addNum(balance);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS);
	}
	@Transactional
	@RequestMapping("/addNums")
	public ResultVO addNums(BalanceInfo balance) {
		int result = BalanceInfoService.addNums(balance);
		if(result == -1) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		if(result <= 0) {
			int result2 = BalanceInfoService.add(balance);
			if(result2 <= 0) {
				return new ResultVO(ResultEnum.ERROR);
			}
		}
		return new ResultVO(ResultEnum.SUCCESS);
	}
}
